#导入模块
import re #蓝图模块
from flask import Blueprint,request #导入request
from db import db1  #导入db文件
from flask.json import jsonify
import json #导入json

#实例化
bp_coures = Blueprint("course",__name__)

#接口
#                  接口               添加   获取   修改    删除
@bp_coures.route('/couponM',methods=["POST","GET","PUT","DELETE"])
def couponM():
    #添加
    if request.method == "POST":
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
         #构造sql语句
        sql ="insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['stime'],data['etime'])
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    #获取    
    elif request.method == 'GET':
        #根据id获取信息
        id = request.args.get('id')
        sql = "select * from coupon where id=%d" % (int(id))#id属于强转int类型
        res = db1.find(sql)
        return jsonify({"code":200,'coupon':res})
    #修改
    elif request.method == 'PUT':
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造sql语句  update 表名 set 字段名1=值  where id=1
        sql = "update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"%(data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        db1.insert(sql)
        return jsonify({"code":200})
    #删除
    elif request.method  == 'DELETE':
        id = request.args.get('id')
        sql ="delete from coupon where id=%d" %(int(id))
        db1.insert(sql)
        return jsonify({"code":200})
        